package com.scwatch.core.thirdapi;

import com.alibaba.fastjson.JSONObject;
import com.scwatch.core.constant.Constants;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 调用第三方api
 * @author gjl
 * @date 2018.8.21
 */
@Component
public class UserSystemApi {

    private String  creditsApi= Constants.CREDITSAPI;



    public JSONObject getCreditsPost(String path, Map<String, Object> params) {
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            sb.append("&" + entry.getKey() + "=" + entry.getValue());
        }

        return ThirdHttpRequestUtils.httpPost(creditsApi + path, sb.substring(1));
    }


    /**
     * 投稿审核通过增加积分(http调用第三方系统)
     * @param userId 用户id（手机注册用户，手机号码）
     * @param token 用户token
     * @return
     */
    public String addIntegral(String userId,String token) {
        //sign 加密，用于系统相互验证，握手
        try {
            String path = "/addIntegral";
            Map<String, Object> params = new HashedMap();
            //ruleType 积分规则页面设置的值一样，1:签到;2:评论;3:转发;4:投稿;5:采用投稿;6:邀请码分享;7:新用户注册;8:输入邀请码
            params.put("userId",userId);
            params.put("token",token);
            params.put("ruleType",5);
            String result=getCreditsPost(path, params).toString();
            return result;
        } catch (Exception e) {
            System.out.println("api name:addIntegral error:"+e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject selectProductList(Map<String, Object> params) {
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            sb.append("&" + entry.getKey() + "=" + entry.getValue());
        }

        return ThirdHttpRequestUtils.httpPost(creditsApi + "/selectProductList", sb.substring(1));
    }

}
